import { createSlice, PayloadAction } from '@reduxjs/toolkit'

const initialState = {
loginState: localStorage.getItem('loginState') === 'true',
adminname: localStorage.getItem('adminname') || '',
token: localStorage.getItem('token') || '',
role: Number(localStorage.getItem('role')),
checkedkeys: JSON.parse(localStorage.getItem('checkedkeys'))
} 

export const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
 changeLoginState (state, action) {
   state.loginState = action.payload
 },
 changeAdminName (state, action) {
   state.adminname = action.payload
 },
 changeToken (state, action) {
   state.token = action.payload
 },
 changeRole (state, action) {
   state.role = action.payload
 },
 changeCheckedkeys (state, action) {
   state.checkedkeys = action.payload
 }
}
})
export const { changeLoginState, changeAdminName, changeToken, changeRole, changeCheckedkeys} = userSlice.actions
export default userSlice.reducer